﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreGallery.Addins.HostView;
using System.AddIn.Hosting;

namespace CoreGallery.Core
{
    public class AddinManager
    {
        private IList<IAcquisitionAddinBase> _acquisitionAddins = new List<IAcquisitionAddinBase>();
        private IList<IPublishingAddinBase> _publishingAddins = new List<IPublishingAddinBase>();
        private IList<IServiceAddinBase> _serviceAddins = new List<IServiceAddinBase>();
        private IList<ITransformAddinBase> _transformAddins = new List<ITransformAddinBase>();
        private IList<IStorageProviderBase> _storageProviders = new List<IStorageProviderBase>();

        public IList<IStorageProviderBase> StorageProviders
        {
            get { return _storageProviders; }
            set { _storageProviders = value; }
        }

        public IList<IAcquisitionAddinBase> Importers
        {
            get { return _acquisitionAddins; }
            set { _acquisitionAddins = value; }
        }

        public IList<IPublishingAddinBase> Exporters
        {
            get { return _publishingAddins; }
            set { _publishingAddins = value; }
        }

        public IList<IServiceAddinBase> Services
        {
            get { return _serviceAddins; }
            set { _serviceAddins = value; }
        }

        public IList<ITransformAddinBase> Transforms
        {
            get { return _transformAddins; }
            set { _transformAddins = value; }
        }

        private List<AppDomain> _toUnload;

        public AddinManager()
        {
            LoadAddIns();
            _toUnload = new List<AppDomain>();

        }

        public void LoadAddIns()
        {
            IList<AddInToken> tokens;
            String path = Environment.CurrentDirectory;
            UnloadAllAddIns();
            AddInStore.Rebuild(path);
            AddInStore.RebuildAddIns(path);

            // Acquisition/importers
            tokens = AddInStore.FindAddIns(typeof(IAcquisitionAddinBase), path);
            foreach (AddInToken token in tokens)
            {
                IAcquisitionAddinBase addin = token.Activate<IAcquisitionAddinBase>(AddInSecurityLevel.FullTrust);
                _acquisitionAddins.Add(addin);
            }

            // Publish/exporters
            tokens = AddInStore.FindAddIns(typeof(IPublishingAddinBase), path);
            foreach (AddInToken token in tokens)
            {
                IPublishingAddinBase addin = token.Activate<IPublishingAddinBase>(AddInSecurityLevel.FullTrust);
                _publishingAddins.Add(addin);
            }

            // Transformations
            tokens = AddInStore.FindAddIns(typeof(ITransformAddinBase), path);
            foreach (AddInToken token in tokens)
            {
                ITransformAddinBase addin = token.Activate<ITransformAddinBase>(AddInSecurityLevel.FullTrust);
                _transformAddins.Add(addin);
            }

            // Services
            tokens = AddInStore.FindAddIns(typeof(IServiceAddinBase), path);
            foreach (AddInToken token in tokens)
            {
                IServiceAddinBase addin = token.Activate<IServiceAddinBase>(AddInSecurityLevel.FullTrust);
                _serviceAddins.Add(addin);
            }

            // Storage providers
            tokens = AddInStore.FindAddIns(typeof(IStorageProviderBase), path);
            foreach (AddInToken token in tokens)
            {
                IStorageProviderBase addin = token.Activate<IStorageProviderBase>(AddInSecurityLevel.FullTrust);
                _storageProviders.Add(addin);
            }
        }

        public void UnloadAllAddIns()
        {
            // Remove from memory collection
            _acquisitionAddins.Clear();
            _publishingAddins.Clear();
            _serviceAddins.Clear();
            _transformAddins.Clear();
            _storageProviders.Clear();
        }

    }
}
